<?php
namespace Mob \ Model;

use Think \ Model;
use Think \ Hook;
/**公告模型
 * Class InfoModel
 */

require_once ('./Application/Mob/Common/function.php');

class CommentModel extends Model {


   public function addComment($commentUid, $businessId, $content,$type,$comment_id)
    {

        //写入数据库
        $data = array('commentUid' => $commentUid,'commentId'=>getSerialno(), 'content' => $content, 'businessId' => $businessId, 'to_comment_id' => $comment_id,'commentType'=>$type);
        $res = $this->add($data);

        if (!$res) return false;
        $comment_id = $data['commentId'];

        switch($type)
        {
        case 1:
         M('Notice')->where(array('noticeId' => $businessId))->setInc('comment_count');
        break;



        }



      //  S('weibo_' . $weibo_id, null);
        //返回评论编号
        return $comment_id;
    }

    public function deleteComment($commentid)
    {
        //获取微博编号

        $comment = $this->queryComment($commentid);
        if ($comment['status'] == 0) {
            return false;
        }

        $type = $comment['commentType'];

        //将评论标记为已经删除

        $this->where(array('commentId' => $commentid))->setField('status', 0);

       switch($type)
       {

       	case 1:
        $noticeId = $comment['businessId'];
        D('Notice')->where(array('noticeId' => $noticeId))->setDec('comment_count');
       	break;


       }



       // S('weibo_' . $weibo_id, null);
        //返回成功结果
        return true;
    }

    public function getComment($commentid){
/*        $comment = S('weibo_comment_'.$id);
        if(!$comment){*/
            $comment = $this->queryComment($commentid);
            $comment['content']=parse_comment_content($comment['content']);
            $comment['user'] = query_user( array('uid', 'nickname', 'avatar32', 'avatar64', 'avatar128'), $comment['commentUid']);
/*            S('weibo_comment_'.$id,$comment);
        }*/
        $comment['can_delete']  = is_administrator(is_login()) || $comment['commentUid'] == is_login();
      return $comment;

    }



    public function getAllComment($businessId){

        $order = modC('COMMENT_ORDER',0,'WEIBO') == 1?'create_time asc':'create_time desc';
        $comment = $this->where(array('businessId'=>$businessId,'status'=>1))->order($order)->field('commentId')->select();
        $ids = getSubByKey($comment,'commentId');
        $list = array();
        foreach($ids as $v){
            $list[$v] = $this->getComment($v);
        }
        return $list;

    }


    public function getCommentList($businessId,$page=1,$show_more =0){

        $order = modC('COMMENT_ORDER',0,'WEIBO') == 1?'create_time asc':'create_time desc';
        $comment = $this->where(array('businessId'=>$businessId,'status'=>1))->order($order)->page($page,10)->field('commentId')->select();
/*        if($page == 1){
            $t = array_chunk($comment,5);
            if($show_more ){
                $comment = $t[1];
            }else{
                $comment = $t[0];
            }
        }*/
        $ids = getSubByKey($comment,'commentId');
        $list = array();
        foreach($ids as $v){
            $list[$v] = $this->getComment($v);
        }
        return $list;

    }


    public function queryComment($commentid)
    {
     return $this->where(array('commentId'=>$commentid))->field('commentId,businessId,commentUid,create_time,content,to_comment_id,status,commentType')->find();


    }

   public function get_CommentList($businessId,$commentType)
   {
    return $this->where(array('businessId'=>$businessId,'commentType'=>$commentType))->field('commentId,businessId,commentUid,create_time,content,to_comment_id,status,commentType')->order('create_time desc')->select();

   }









}